package daily.year2024.m8;

import java.util.HashMap;
import java.util.Map;

public class d12 {
    class Solution {
        public int maxOperations(int[] nums, int k) {
            int n = nums.length;
            Map<Integer,Integer> vals = new HashMap<>();
            int res = 0;
            for(int i=0;i < n;i++) {
                if(vals.containsKey(nums[i])) {
                    res++;
                    int v = vals.get(nums[i]);
                    if(v == 1) {
                        vals.remove(nums[i]);
                    } else {
                        vals.put(nums[i],v-1);
                    }
                } else {
                    vals.put(k-nums[i],vals.getOrDefault(k-nums[i],0)+1);
                }
            }
            return res;
        }
    }
}
